package com.hxm.appGenealogy.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxm.appGenealogy.Function.PeopleFunction;
import com.hxm.appGenealogy.config.Constant;
import com.hxm.appGenealogy.config.ParmIntended;
import com.hxm.appGenealogy.config.client.SdkClient;
import com.hxm.appGenealogy.config.queryVo.QueryWrapperVO;
import com.hxm.appGenealogy.entity.People;
import com.hxm.appGenealogy.entity.PeopleVo;
import com.hxm.appGenealogy.entity.ResponDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController()
@RequestMapping("app/login")
public class LoginController {

    @Autowired
    private PeopleFunction peopleFunction;


    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 微信登录;
     * @param peopleVO
     * @return
     */
    @PostMapping("wxlogin")
    public ResponDataVo wxLogin(@RequestBody PeopleVo peopleVO) {
        //参数信息的教验;
        String parmAnalysis = ParmIntended.parmAnalysis(peopleVO,"openId");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //判断是否存储在openid;
        People people = peopleFunction.queryGetOneString(JSON.toJSONString(new QueryWrapperVO().eq("open_id",peopleVO.getOpenId())));
        if (people == null) {
            People people1 = new People();
            people1.setOpenId(peopleVO.getOpenId());
            people1.setIsVip(0);
            peopleFunction.insertPeople(people1);
            return new ResponDataVo(people1);
        }
        return new ResponDataVo(people);
    }

    @PostMapping("binDing")
    public ResponDataVo binDing(@RequestBody PeopleVo people){
        String parmAnalysis = ParmIntended.parmAnalysis(people, "openId","number","code");
        if(parmAnalysis!=null){
            return new ResponDataVo(parmAnalysis);
        }
        String unmberCode = (String) redisTemplate.opsForValue().get(people.getNumber());
        if(unmberCode==null){
            return new ResponDataVo("请输入验证码;");
        }
        if(!unmberCode.equals(people.getCode())){
            return new ResponDataVo("无效的验证码;");
        }
        People peopleOpenId = peopleFunction.queryGetOneString(JSON.toJSONString(new QueryWrapperVO().eq("open_id", people.getOpenId())));
        if(peopleOpenId==null){
            return new ResponDataVo("请先使用微信登录后在绑定手机号;");
        }
        List<People> peopleList = peopleFunction.queryGeListString(JSON.toJSONString(new QueryWrapperVO()));
        for (People people1 : peopleList) {
            if(people1.getNumber()!=null){
                if(people1.getNumber().equals(people.getNumber())){
                    return new ResponDataVo("当前的手机号已经存在");
                }
            }

        }
        peopleOpenId.setNumber(people.getNumber());
        peopleFunction.updateByid(peopleOpenId);
        return new ResponDataVo(peopleOpenId);
    }


    /**
     * 手机号登录;
     */
    @PostMapping("numverlogin")
    public ResponDataVo unmberLogin(@RequestBody PeopleVo peopleVo) {
        //参数教验,手机号还有验证码;
        String parmAnalysis = ParmIntended.parmAnalysis(peopleVo, Constant.umbber, Constant.verificationCode);
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        String unmberCode = (String) redisTemplate.opsForValue().get(peopleVo.getNumber());
        if (unmberCode == null) {
            return new ResponDataVo("无效的验证码;");
        }
        if (!unmberCode.equals(peopleVo.getVerificationCode())) {
            return new ResponDataVo("验证码错误;");
        }
        QueryWrapperVO  queryWrapper = new QueryWrapperVO();
        queryWrapper.eq(Constant.umbber,peopleVo.getNumber());
        People people = peopleFunction.queryGetOneString(JSON.toJSONString(queryWrapper));
        People people1 = null;
        if(people!=null){
            return new ResponDataVo(people);
        }else {
            people1 = new People();
            people1.setNumber(peopleVo.getNumber());
            people1.setIsVip(0);
            peopleFunction.insertPeople(people1);
        }
        return new ResponDataVo(people1);
    }
    /**
     * 账号密码登录;
     */
    @PostMapping("pswlogin")
    public ResponDataVo pswLogin(@RequestBody People people){
        String parmAnalysis = ParmIntended.parmAnalysis(people, "number", "psaaworld");
        if(parmAnalysis!=null){
            return new ResponDataVo("请输入账号或者密码");
        }
        //根据手机号查询出来这个人的信息;
        People peopleNumber = peopleFunction.queryGetOneString(JSON.toJSONString(new QueryWrapperVO().eq("number", people.getNumber())));
        if(peopleNumber==null){
            return new ResponDataVo("没有当前的账号信息");
        }
        if(!peopleNumber.getPassWorld().equals(people.getPassWorld())){
            return new ResponDataVo("账号或者是密码错误");
        }
        return new ResponDataVo(peopleNumber);
    }
}









